/**
 * @param {number} n
 * @return {string[]}
 */
var generateParenthesis = function (n) {
  let result = []
  let path = []
  function backTracking (left, right) {
    if (left === 0 && right === 0) {
      result.push(path.join(''))
      return
    }
    if (right === 0) { // 如果右边没有了 就直接返回
      return
    }
    if (left > 0) {
      left--
      path.push('(')
      backTracking(left, right)
      path.pop()
      left++
    }

    if (right > 0 && left < right) {
      right--
      path.push(')')
      backTracking(left, right)
      path.pop()
      right++
    }
  }
  backTracking(n, n)
  return result
};
console.log(generateParenthesis(3));